﻿@Cofe ?= {}

$ = jQuery
Event = Cofe.Event
Feed = Cofe.Feed
EventItem = Cofe.EventItem
utils = Cofe.utils

# CONTROLLER - Represents a Feed.
#     params - @el, @templateKey (css template), @feed (Feed model)
#     events - clicked
class FeedItem extends Spine.Controller

  constructor : (constructParams) ->        
    super   
    @refresh(false)
    @feed.bind("refreshed", @onFeedRefreshed)
  
  FeedItem.fromUrl = (url) ->
    feed = Feed.fromUrl(url)
    FeedItem.fromFeed(feed)
  
  FeedItem.fromFeed = (feed) ->
    new FeedItem({      
      feed : feed
    })
  
  load : () =>
    @feed.fetch()

  
  next : () => 
    FeedItem.fromUrl(@feed.links.nextArchive.url)
  prev : () => 
    FeedItem.fromUrl(@feed.links.prevArchive.url)

  refresh : (triggerRefreshed) =>    
    @eventItems = (EventItem.fromEvent(e) for e in @feed?.events)    
    @hasNext = @feed.links.nextArchive isnt undefined
    @hasPrev = @feed.links.prevArchive isnt undefined
    
    if triggerRefreshed 
      @trigger("refreshed", @)
      @trigger("loaded", @)

  onFeedRefreshed : () =>
    @refresh(true)
    
    

#  template: (templateKey) ->
#    if templateKey is undefined then throw "templateKey not specified"
#    templateKey.render(@event)
  
   
Cofe.FeedItem = FeedItem